{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d369414a-8ea5-42df-8b6e-4fa52937aaa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f0b4d3f-5bf4-4be3-8cd3-6ae41a32a706",
   "metadata": {},
   "source": [
    "**步骤1：创建子类**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "47c59b8d-3e67-42ef-aeed-a0a3e4f6115e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class TYW(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "    def forward(self,inputx):\n",
    "        output = inputx+1\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "79f9b0a0-6b17-452c-bb01-e648efcd44c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.)\n",
      "tensor(2.)\n"
     ]
    }
   ],
   "source": [
    "tyw = TYW()\n",
    "x = torch.tensor(1.0)\n",
    "print(x)\n",
    "output = tyw(x) ## 能够这样使用的原因是，__call__函数中会调用forward\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abfe0965-139a-4e6e-9fee-dd069ddab8b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
